﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace DataAccessLayer
{
    public class Dal
    {
        public SqlConnection Connection { get; set; }

        public Dal()
        {
            Connection = new SqlConnection(@"Data Source=RENATO-PC;Initial Catalog=GICMC_dataBase;Integrated Security=true;");
        }

        public bool ExecutaSql(string sql)
        {
            try
            {
                Connection.Open();

                SqlCommand comm = new SqlCommand(sql, Connection);
                comm.CommandType = CommandType.StoredProcedure;
                comm.ExecuteNonQuery();

                Connection.Close();

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Connection.State == ConnectionState.Open)
                {
                    Connection.Close();
                }
            }
        }

        public DataSet ExecutaLeitura(string sql)
        {
            DataSet ds = new DataSet();

            try
            {
                Connection.Open();

                SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
                da.Fill(ds);

                Connection.Close();
            }
            catch (Exception ex)
            {
                if (Connection.State == ConnectionState.Open)
                {
                    Connection.Close();

                }
            }
            return ds;
        }
    }
}
